#include "stdio.h"
#include <libgen.h>

typedef struct _my_time
{
	int h;
	int m;
	
} mytime;

void usage(char *name)
{
	char *bn = basename(name);
	printf("%s --- help ---\n", name);
	printf("%s 20:21 20:22\n", bn);
}

int timecompare(mytime t1, mytime t2)
{
	if (t1.h > t2.h)
		return 1;
	if (t1.h < t2.h)
		return -1;
	if (t1.m > t2.m)
		return 1;
	if (t1.m < t2.m)
		return -1;
	return 0;
}

void formatminite(char *buf, int m)
{
	int hr, mr;
	float fh;

	hr = m/60;
	fh = (float)(m%60)/60.0f;
	fh *= 1000;
	mr = fh;
	sprintf(buf, "%d.%03d\n", hr, mr);
}

int timeperiodminites(mytime t2, mytime t1)
{
	int min1, min2;

	min2 = t2.h * 60 + t2.m;
	min1 = t1.h * 60 + t1.m;
	return min2 - min1;
}


int main(int argc, char *argv[])
{
	char hours_point_minites[20];
	char rest_noon[20] = {0};
	char rest_afternoon[20] = {0};
	int rest_noon_m = 1*60 + 30;
	int rest_afternoon_m = 1*60 + 0;
	int h, m;
	mytime t_noon, t_afternoon;
	mytime t1, t2, tt;
	int mlong;

	if (argc < 3) {
		printf("arguments invalid\n");
		usage(argv[0]);
		return -1;
	}
	if (sscanf(argv[1], "%d:%d", &t1.h, &t1.m) != 2) {
		printf("time1 invalid!!!\n");
		usage(argv[0]);
		return -1;
	}
	if (sscanf(argv[2], "%d:%d", &t2.h, &t2.m) != 2) {
		printf("time2 invalid!!!\n");
		usage(argv[0]);
		return -1;
	}

	sprintf(rest_noon, "12:00");
	sprintf(rest_afternoon, "18:00");
	sscanf(rest_noon, "%d:%d", &t_noon.h, &t_noon.m);
	sscanf(rest_afternoon, "%d:%d", &t_afternoon.h, &t_afternoon.m);
	printf("time2[%d:%d] - time1[%d:%d]\n", t2.h, t2.m, t1.h, t1.m);
	printf("rest_afternoon[%d:%d] - time1[%d:%d]\n", t_noon.h, t_noon.m, t_afternoon.h, t_afternoon.m);
	mlong = timeperiodminites(t2, t1);
	formatminite(hours_point_minites, mlong);
	printf("time period is %s\n", hours_point_minites);
	if(timecompare(t1, t2) >= 0) {
		tt = t1;
		t1 = t2;
		t2 = tt;
	}
	if (timecompare(t1, t_noon) <= 0 && timecompare(t2, t_afternoon) >= 0) {
		mlong = mlong - rest_noon_m - rest_afternoon_m;
		formatminite(hours_point_minites, mlong);
		printf("time period after reset is %s\n", hours_point_minites);
	}
	return 0;
}
